<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/16
 * Time: 11:08
 */

namespace zhtfrontend\modules\teachergrowth\controllers;

use common\components\helpers\Fun;
use common\models\datas\Message;
use zhtfrontend\modules\teachergrowth\components\rest\ActiveController;
use yii\web\Response;
use yii\helpers\ArrayHelper;

class MessageController extends ActiveController

{
    private $_config;
    public $modelClass = 'zhtfrontend\models\datas\ClassManage';

    public $serializer = [
        'class' => 'zhtfrontend\modules\teachergrowth\components\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        return $behaviors;
    }

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['create'], $actions['update'], $actions['delete'], $actions['view']);
        return $actions;
    }

    //系统通知
    public function actionSysmessagelist()
    {
        $data = \Yii::$app->request->post();
        $role = ArrayHelper::keyExists('role', $data) ? $data['role'] : '';
        $receiver_id = ArrayHelper::keyExists('receiver_id', $data) ? $data['receiver_id'] : '';
//        if($role == 'class_director'){
//            $role = 'class_teacher';
//        }elseif ($role == 'teacher'){
//            $role = 'teacher';
//        }
        $site_id = Fun::getSiteId();
        $sysMessage = Message::find()
            ->where(['send_group' => $role, 'receiver_id' => $receiver_id, 'site_id' => $site_id])
            ->orderBy('created_at DESC')
            ->asArray()
            ->all();
        if (empty($sysMessage)) {
            return ['statusCode' => 0, 'message' => 'error', 'result' => '暂时没有消息'];
        } else {
            return ['statusCode' => 200, 'message' => 'success', 'items' => $sysMessage];
        }
    }


    //详细信息
    public function actionMessagedetails()
    {
        $data = \Yii::$app->request->post();
        $message_id = ArrayHelper::keyExists('message_id', $data) ? $data['message_id'] : '';
        $site_id = Fun::getSiteId();
        $message = Message::find()
            ->where(['site_id' => $site_id, 'id' => $message_id])
            ->one();
        if (empty($message)) {
            return ['statusCode' => 0, 'message' => 'error', 'result' => '暂时没有消息'];
        } else {
            $message->status = 1;
            if ($message->save(false)) {
                $data_msg = [
                    'id' => $message->id,
                    'receiver_id' => $message->receiver_id,
                    'content' => $message->content,
                    'created_at' => $message->created_at,
                    'status' => $message->status,
                ];
                return ['statusCode' => 200, 'message' => 'success', 'items' => $data_msg];
            }else{
                return ['statusCode' => 0, 'message' => 'success', 'result' => '暂时没有消息'];
            }

        }

    }


}